package com.example.lotterysystem.dao.mapper;

import com.example.lotterysystem.dao.dataobject.Encrypt;
import com.example.lotterysystem.dao.dataobject.UserDO;
import com.example.lotterysystem.service.dto.UserDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    /**
     * 查询邮箱绑定的人数
     *
     * @param email
     * @return
     */
    @Select("select count(*) from user where email = #{email}")
    int countByEmail(@Param("email") String email);

    /**
     * 查询邮箱绑定的人数
     *
     * @param phoneNumber
     * @return
     */
    @Select("select count(*) from user where phone_number = #{phoneNumber}")
    int countByPhone(@Param("phoneNumber") Encrypt phoneNumber);

    /**
     * 新增用户
     *
     * @param userDO
     */
    @Insert("<script>" +
            "INSERT INTO user (user_name, email, phone_number, password, identity" +
            "<if test='createId != null'>, create_id</if>)" +
            " VALUES (#{userDO.userName}, #{userDO.email}, #{userDO.phoneNumber}, #{userDO.password}, #{userDO.identity}" +
            "<if test='createId != null'>, #{createId}</if>)" +
            "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "userDO.id", keyColumn = "id")
    void insert(@Param("createId") Integer createId, @Param("userDO") UserDO userDO);


    /**
     * 根据邮箱查询用户信息
     *
     * @param email
     */
    @Select("select * from user where email = #{email}")
    UserDO selectUserInfoByEmail(@Param("email") String email);

    /**
     * 根据手机号查询用户信息
     *
     * @param phoneNumber
     */
    @Select("select * from user where phone_number = #{phoneNumber}")
    UserDO selectUserInfoByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    /**
     * 根据用户身份查询用户信息
     *
     * @param identity
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM user " +
            "<where> " +
            "   <if test='createId != null'> " +
            "       AND create_id = #{createId} " +
            "   </if> " +
            "   <if test='identity != null'> " +
            "       AND identity = #{identity} " +
            "   </if> " +
            "</where> " +
            "ORDER BY id DESC " +
            "</script>")
    List<UserDO> selectUserInfoByIdentity(@Param("createId") Integer createId,
                                          @Param("identity") String identity);

    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectUserInfoById(@Param("items") List<Long> ids);


    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items") List<Long> ids);
}
